﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

public class FadeOutEnv : EnvBase
{
    // Member vars
    private Texture2D m_prevScreen;
    private double m_fullTimer;
    private double m_timer;
    private EnvBase m_target;

    /**
     * Our constructor.
     * @param target - Env we should transition to.
     * @param millisToFade - Time before we transition.
     */
    public FadeOutEnv(EnvBase target, double millisToFade)
    {
        m_prevScreen = Game.getGame().getEnvStack().currentEnvAsTexture();
        m_fullTimer = millisToFade;
        m_timer = millisToFade;
        m_target = target;
    }

    /**
     * Counts down our timer and goes to next Env if necessary.
     */
    override public void update(double dt)
    {
        m_timer -= dt;
        if (m_timer <= 0.0)
        {
            m_parentStack.popOnce(m_target);
        }
    }

    /**
     * Renders our screen as a function of the time left in fading.
     */
    override public void render()
    {
        Color color = Color.White;
        color.A = (byte)(255 * (m_timer / m_fullTimer));
        Game.getGame().getSpriteBatch().Draw(m_prevScreen, new Vector2(0, 0), null, color, 0.0f,
            new Vector2(0, 0), 1.0f, SpriteEffects.None, Priority.kHUDNear);
    }
}
